<?php
namespace app\indexLogin\controller;

use app\indexLogin\common\Common;
use app\indexLogin\model\Article;
use app\indexLogin\model\ArticleTag;
use app\indexLogin\model\Weibo;
use think\Controller;
use think\Db;
use think\Request;
use \think\Session;

class Index extends Common{
    //首页
     public function index(){
         //存入
         Session::set('uid',1);
         //获去当前用户的uid
         $uid=Session::get('uid');
         $this->assign('uid',$uid);

//         调取微博数据、
         $weibos=Db::table('wb_weibo')->select();
//         dd($weibos);die;
//         追加点赞数,username,头像
         foreach($weibos as $k=>$v){
             $weibos[$k]['nices']=Db::table('wb_weiboNice')->where('wid','=',$v['wid'])->count();
             $weibos[$k]['username']=Db::table('wb_user')->where('uid','=',$v['uid'])->value('username');
             $weibos[$k]['avatar']=Db::table('wb_userinfo')->where('uid','=',$v['uid'])->value('avatar');
             if(Db::table('wb_store')->where('uid','=',$uid)->where('wid','=',$v['wid'])->select()){
//                 表示该文章已经被收藏
                 $weibos[$k]['isstore']=1;
             }else{
                 $weibos[$k]['isstore']=0;
             }
             if(Db::table('wb_weiboNice')->where('uid','=',$uid)->where('wid','=',$v['wid'])->select()){
//                 表示该微博已经被该用户点过赞
                 $weibos[$k]['isnice']=1;
             }else{
                 $weibos[$k]['isnice']=0;
             }
         }
         dd($weibos);
         $this->assign('weibos',$weibos);
      return view();
     }

    /**
     * 异步发布微博
     */
    public function pub(){
        if(Request::instance()->isPOST()){
             $uid=Session::get('uid');
            $data['pubtime']=time();
            $data['uid']=$uid;
            $data['content']=$_POST['editorValue'];
            $res=(new Weibo())->add($data);
            if($res){
                $data=json_encode($data);
                echo $data;
            }else{
               echo 0;
            }
        }

    }
    /**
     * 异步发布文章
     */
    public function pubarc(){
        //找出分类
        $category=Db::table('wb_category')->select();
        $this->assign('category',$category);
        //找出标签
        $tags=Db::table('wb_tag')->select();
        $this->assign('tags',$tags);
        if(Request::instance()->isPOST()){
            $result = $this->validate(
                [
                    'title'  => $_POST['title'],
                    'cid' => $_POST['cid'],
                ],
                [
                    'title'  => 'require|max:64',
                    'cid'   => 'require',
                ]);
            if(!isset($_POST['tid'])){
                return ['valid'=>false,'msg'=>'前选择标签'];
            }
            if(true !== $result){
                // 验证失败 输出错误信息
                return ['valid'=>false,'msg'=>$result];
            }else{
                //向文章表插入数据
                $data['content']=$_POST['editorValue'];
                $data['title']=$_POST['title'];
                $data['cid']=$_POST['cid'];
                $data['pubtime']=time();
                $data['uid']=1;
                $aid=(new Article())->add($data);

                //想标签中间表中插入数据
                $at['aid']=$aid;
                $at['tid']=$_POST['tid'];
                $res=(new ArticleTag())->add($at);
                return $res;

            }


        }
    return view();
    }

    /**
     * 关注
     */
    public  function focus(){
        $fid=Request::instance()->param('uid');
        $uid=Session::get('uid');
        $data = ['uid' =>$uid, 'fid' => $fid];
        $res=Db::table('wb_focus')->insert($data);
        if($res){
            $this->success('关注成功');
        }else{
            $this->error('关注失败');
        }
    }

}